home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / lang / ThreadLocal.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.1 KB  |  124 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)ThreadLocal.java    1.8 98/07/08
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16. import java.util.*;
  17.  
  18. /**
  19.  * This class provides ThreadLocal variables.  These variables differ from
  20.  * their normal counterparts in that each thread that accesses one (via its
  21.  * get or set method) has its own, independently initialized copy of the
  22.  * variable.  ThreadLocal objects are typically private static variables in
  23.  * classes that wish to associate state with a thread (e.g., a user ID or
  24.  * Transaction ID).
  25.  * <p>
  26.  * Each thread holds an implicit reference to its copy of a ThreadLocal
  27.  * as long as the thread is alive and the ThreadLocal object is accessible;
  28.  * after a thread goes away, all of its copies of ThreadLocal variables are
  29.  * subject to garbage collection (unless other references to these copies
  30.  * exist).
  31.  *
  32.  * @author  Josh Bloch
  33.  * @version 1.8 07/08/98
  34.  * @since   JDK1.2
  35.  */
  36.  
  37. public class ThreadLocal {
  38.     /**
  39.      * Maps each Thread that has a value for this ThreadLocal to an Entry
  40.      * containing its value.  The initial size is 53 because that is twice
  41.      * the maximum concurrency that one might reasonably expect.
  42.      */
  43.     Map map = Collections.synchronizedMap(new WeakHashMap(53));
  44.  
  45.     /**
  46.      * Creates a ThreadLocal variable.
  47.      */
  48.     public ThreadLocal() {
  49.     }
  50.  
  51.     /**
  52.      * Returns the calling thread's initial value for this ThreadLocal
  53.      * variable. This method will be called once per accessing thread for
  54.      * each ThreadLocal, the first time each thread accesses the variable
  55.      * with get or set.  If the programmer desires ThreadLocal variables
  56.      * to be initialized to some value other than null, ThreadLocal must
  57.      * be subclassed, and this method overridden.  Typically, an anonymous
  58.      * inner class will be used.  Typical implementations of initialValue
  59.      * will call an appropriate constructor and return the newly constructed
  60.      * object.
  61.      */
  62.     protected Object initialValue() {
  63.     return null;
  64.     }
  65.  
  66.     /**
  67.      * Returns the value in the calling thread's copy of this ThreadLocal
  68.      * variable.  Creates and initializes the copy if this is the first time
  69.      * the thread has called this method.
  70.      */
  71.     public Object get() {
  72.     Entry ourEntry = ourEntry(true);
  73.     return ourEntry.value;
  74.     }
  75.  
  76.     /**
  77.      * Sets the calling thread's instance of this ThreadLocal variable
  78.      * to the given value.  This is only used to change the value from
  79.      * the one assigned by the initialValue method, and many applications
  80.      * will have no need for this functionality.
  81.      *
  82.      * @param value the value to be stored in the calling threads' copy of
  83.      *          this ThreadLocal.
  84.      */
  85.     public void set(Object value) {
  86.     Entry ourEntry = ourEntry(false);
  87.         ourEntry.value = value;
  88.     }
  89.  
  90.     /**
  91.      * Returns the calling thread's Entry for this ThreadLocal, creating a new
  92.      * one and putting it in the map if none exists.  If initUponCreate is
  93.      * true, this call will use initialValue() to set the value in a newly
  94.      * created Entry; otherwise, it will set the value in a newly created
  95.      * Entry to null.
  96.      */
  97.     private Entry ourEntry(boolean initUponCreate) {
  98.     Thread ourThread = Thread.currentThread();
  99.     Entry ourEntry = (Entry)(map.get(ourThread));
  100.     if (ourEntry == null) {
  101.             ourEntry = newEntry(initUponCreate ? initialValue() : null);
  102.         map.put(ourThread, ourEntry);
  103.         }
  104.         return ourEntry;
  105.     }
  106.  
  107.     /**
  108.      * This factory method is overriden by InheritableThreadLocal to
  109.      * specialize behavior.
  110.      */
  111.     Entry newEntry(Object value) {
  112.         return new Entry(value);
  113.     }
  114.  
  115.     /**
  116.      * The value associated with a (ThreadLocal, Thread) pair.
  117.      */
  118.     static class Entry {
  119.         Entry(Object value) {this.value = value;}
  120.  
  121.         Object value;
  122.     }
  123. }
  124.